package com.biomatch.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.biomatch.domain.Fingerprint;
import com.biomatch.domain.Position;
import com.biomatch.domain.UserInfo;
import com.biomatch.dto.FingerprintCount;
import com.biomatch.dto.Pagination;
import com.biomatch.response.RestResponse;
import com.biomatch.response.RestResult;
import com.biomatch.service.FingerprintService;
import com.biomatch.service.UserService;
import com.biomatch.utils.Internationalize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController("lock.fingerprint")
@RequestMapping("/fingerprint")
@Api(value="指纹管理")
public class FingerprintController {
    @Autowired
    private FingerprintService fingerprintService;

    @Autowired
    private Internationalize internationalize;

    @RequestMapping(value="/getFingerprintByPage", method = RequestMethod.POST)
    @ApiOperation(value = "分页获取指纹信息", response = Fingerprint.class, httpMethod = "POST")
    public RestResult<Fingerprint> getFingerprintByPage(@RequestBody Pagination pagination) {
        IPage<Fingerprint> result =fingerprintService.getFingerprintByPage(pagination);
        return RestResponse.success(result);
    }

    @RequestMapping(value="/getAllFingerprint", method = RequestMethod.POST)
    @ApiOperation(value = "获取全部指纹信息", response = Fingerprint.class, httpMethod = "POST")
    public RestResult<List<Fingerprint>> getAllFingerprint() {
        List<Fingerprint> result=fingerprintService.getAllFingerprint();
        return RestResponse.success(result);
    }

    @RequestMapping(value="/getFingerprintCountAndUsersByPage", method = RequestMethod.POST)
    @ApiOperation(value = "获取分页用户信息和指纹个数", response = Fingerprint.class, httpMethod = "POST")
    public RestResult<FingerprintCount> getFingerprintCountAndUsersByPage(@RequestBody Pagination pagination) {
        IPage<FingerprintCount> result =fingerprintService.getFingerprintCountAndUsersByPage(pagination);
        return RestResponse.success(result);
    }

    @RequestMapping(value="/FindFingerPrintsByUser", method = RequestMethod.POST)
    @ApiOperation(value = "根据用户查找对应的指纹详情", response = Fingerprint.class, httpMethod = "POST")
    public RestResult<List<Fingerprint>> FindFingerPrintsByUser(@RequestBody FingerprintCount fingerprintCount) {
        List<Fingerprint>  result =fingerprintService.FindFingerPrintsByUser(fingerprintCount.getUserId());
        return RestResponse.success(result);
    }


        @RequestMapping(value="/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "新增或更新指纹信息", response = Fingerprint.class, httpMethod = "POST")
    public RestResult<Fingerprint> saveOrUpdate(@RequestBody Fingerprint fingerprint) {
        Fingerprint result = fingerprintService.saveOrUpdate(fingerprint);
        if (result == null) {
            return RestResponse.error(internationalize.getMessage("fingerprint.name.repeat"));
        } else {
            return RestResponse.success(result);
        }
    }

    @RequestMapping(value="/deleteFingerprint", method = RequestMethod.POST)
    @ApiOperation(value = "删除全部指纹信息", response = Fingerprint.class, httpMethod = "POST")
    public RestResult<Fingerprint> deleteAllFingerPrints(@RequestBody Fingerprint fingerprint) {
        Fingerprint result = fingerprintService.deleteAllFingerPrints(fingerprint);
        if (result == null) {
            return RestResponse.error(internationalize.getMessage("fingerprint.status.used"));
        } else {
            return RestResponse.success(result);
        }
    }

    @RequestMapping(value="/deleteOneFingerprintById", method = RequestMethod.POST)
    @ApiOperation(value = "根据id删除指纹", response = Fingerprint.class, httpMethod = "POST")
    public RestResult<Fingerprint> deleteOneFingerprintById(@RequestBody Fingerprint fingerprint) {
        Fingerprint result = fingerprintService.deleteOneFingerprintById(fingerprint);
        if (result == null) {
            return RestResponse.error(internationalize.getMessage("fingerprint.status.used"));
        } else {
            return RestResponse.success(result);
        }
    }
}
